package com.jie.other.jetpack.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import androidx.lifecycle.ViewModel
import com.jie.other.bean.ui.ErrorBean
import com.jie.other.bean.ui.ViewModelItemBean
import com.jie.other.jetpack.repository.UserListRepository

class UserViewModel : ViewModel() {

    private var mLoading: MutableLiveData<Boolean> = MutableLiveData()
    private var mRequestResult: MutableLiveData<ErrorBean> = MutableLiveData()
    private var mBtnVisible: MediatorLiveData<Boolean> = MediatorLiveData()
    private var mUserList : MutableLiveData<MutableList<ViewModelItemBean>> = MutableLiveData()

    private var mContentList : MutableList<ViewModelItemBean> = ArrayList()
    private var mContentListChange : MutableLiveData<Int> = MutableLiveData()
    private var mFirstContent : LiveData<ViewModelItemBean> = Transformations.map(mContentListChange) {
        if (mContentList.isNotEmpty()) {
            mContentList[0]
        } else {
            null
        }
    }

    init {
        mBtnVisible.addSource(mRequestResult) {
            mBtnVisible.value = it.code != 0
        }
        mBtnVisible.addSource(mLoading) {
            if (it) {
                mBtnVisible.value = !it
            }
        }
        mUserList.value = ArrayList()
    }

    fun getLoading() : LiveData<Boolean> {
        return mLoading
    }

    fun getUserList(): LiveData<MutableList<ViewModelItemBean>> {
        return mUserList
    }

    fun getContentList(): MutableList<ViewModelItemBean> {
        return mContentList
    }

    fun getContentListChange() : LiveData<Int> {
        return mContentListChange
    }

    fun getFirstContent() : LiveData<ViewModelItemBean> {
        return mFirstContent
    }

    fun getRequestResult() : LiveData<ErrorBean> {
        return mRequestResult
    }

    fun getBtnVisible() : LiveData<Boolean> {
        return mBtnVisible
    }

    fun loadUserList() {
        mLoading.value = true
        UserListRepository.loadUserList(
            {
                if (mUserList.value.isNullOrEmpty()) {
                    mUserList.postValue(it.toMutableList())
                    mRequestResult.postValue(ErrorBean())
                }
                mLoading.postValue(false)
            },
            {
                mRequestResult.postValue(ErrorBean(1, it))
                mLoading.postValue(false)
            }
        )
    }

    fun selectUser(index : Int) {
        if (index >= 0 && index < (mUserList.value?.size ?: 0)) {
            if (mUserList.value?.get(index)?.isSelect == false) {
                mUserList.value?.forEachIndexed { i, viewModelItemBean ->
                    viewModelItemBean.isSelect = i == index
                }
                mUserList.postValue(mUserList.value)
            }
        }
    }

    fun addUser(user : ViewModelItemBean) {
        mContentList.add(user)
        mContentListChange.value = mContentListChange.value?.inc()?:0
    }

    fun removeUser(index : Int) {
        if (index >= 0 && index < mContentList.size) {
            mContentList.removeAt(index)
            mContentListChange.value = mContentListChange.value?.dec()?:0
        }
    }

}